// component/hotZone/hotZone.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data:{
      type: Object,
      // 数据类型
      value:{}
      // 数据值
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgUrl:getApp().globalData.imgUrl,
    hotZoneList: {},
    leftContentData: [], // 左边区域商品数据
    rightContentData: [], // 右边区域商品数据
    leftCountDown: { day : "00", hour: "00", min: "00", sec: "00" },     // 倒计时
    rightCountDown: { day : "00", hour: "00", min: "00", sec: "00" },     // 倒计时
  },
  attached: function(){
    let hotZoneList = this.properties.data
    let leftContentData = []
    let rightContentData = []
    if (hotZoneList && hotZoneList.leftContentData) {
      if (hotZoneList.leftContentType == 'spellBuy') {
        leftContentData = hotZoneList.leftContentData.buySkuList
      } else if (hotZoneList.leftContentType == 'cutPrice') {
        leftContentData = hotZoneList.leftContentData.priceSkuList
      } else if (hotZoneList.leftContentType == 'seckill') {
        leftContentData = hotZoneList.leftContentData.products
      } else if (hotZoneList.leftContentType == 'specialColumn') {
        leftContentData = hotZoneList.leftContentData.solrProductDtos.records
      }
    }
    if (hotZoneList && hotZoneList.rightContentData) {
      if (hotZoneList.rightContentType == 'spellBuy') {
        rightContentData = hotZoneList.rightContentData.buySkuList
      } else if (hotZoneList.rightContentType == 'cutPrice') {
        rightContentData = hotZoneList.rightContentData.priceSkuList
      } else if (hotZoneList.rightContentType == 'seckill') {
        rightContentData = hotZoneList.rightContentData.products
      } else if (hotZoneList.rightContentType == 'specialColumn') {
        rightContentData = hotZoneList.rightContentData.solrProductDtos.records
      }
    }
    this.setData({
      hotZoneList: hotZoneList,
      leftContentData: leftContentData,
      rightContentData: rightContentData,
    })
    if (hotZoneList.leftContentData) {
      this.setData({
        leftStatus: hotZoneList.leftContentData.status,
        leftendTime: hotZoneList.leftContentData.endTime || ''
      })
      // if (hotZoneList.leftContentData.endTime) {
      //   this.setData({
      //   })
      // }
    }
    if (hotZoneList.rightContentData) {
      this.setData({
        rightendTime: hotZoneList.rightContentData.endTime || '',
        rightStatus: hotZoneList.rightContentData.status
      })
    }
    // console.log('左边区域leftContentData', this.data.leftContentData)
    // console.log('右边区域rightContentData', this.data.rightContentData)
    if (this.data.leftendTime) {
      this.leftCountDown()
    }
    if (this.data.rightendTime) {
      this.rightCountDown()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 左边区域跳转
    leftPage() {
      let linkType = this.data.hotZoneList.leftLinkType
      let linkAppId =  this.data.hotZoneList.leftLinkAppId
      let linkUrl = this.data.hotZoneList.leftLinkUrl
      if (linkType == 'otherMiniProgram') { // 其他小程序跳转
        this.goMeita(linkAppId)
      } else if (linkType == 'miniPage') {
        if (linkUrl == '/packagBuy/pages/marketing/seckill/seckill') {
          wx.navigateTo({
            url: '/packagBuy/pages/marketing/seckill/seckill?status='+this.data.leftStatus
          })
        } else {
          wx.navigateTo({
            url: linkUrl
          })
        }
      } else if(linkType == 'h5' && linkUrl!== '/packagBuy/pages/found/crmweb/index') {
        wx.navigateTo({
          url: '/packagBuy/pages/marketing/poster/posterh5/posterh5?h5Url=' + linkUrl
        })
      } else if(linkUrl == '/packagBuy/pages/found/crmweb/index') {
        wx.navigateTo({
          url: '/packagBuy/pages/found/crmweb/index'
        })
      }
    },
    // 右边区域跳转
    rightPage() {
      let linkType = this.data.hotZoneList.rightLinkType
      let linkAppId =  this.data.hotZoneList.rightLinkAppId
      let linkUrl = this.data.hotZoneList.rightLinkUrl
      if (linkType == 'otherMiniProgram') { // 其他小程序跳转
        this.goMeita(linkAppId)
      } else if (linkType == 'miniPage') {
        wx.navigateTo({
          url: linkUrl
        })
      } else if(linkType == 'h5' && linkUrl!== '/packagBuy/pages/found/crmweb/index') {
        wx.navigateTo({
          url: '/packagBuy/pages/marketing/poster/posterh5/posterh5?h5Url=' + linkUrl
        })
      } else if(linkUrl == '/packagBuy/pages/found/crmweb/index') {
        wx.navigateTo({
          url: '/packagBuy/pages/found/crmweb/index'
        })
      }
    },
      // 美搭小程序跳转
    goMeita(linkAppId) {
      wx.navigateToMiniProgram({
        appId: linkAppId,//小程序appid
        path: '',//跳转关联小程序app.json配置里面的地址
        extraData: {
          foo: 'bar'
        },
      //**重点**要打开的小程序版本，有效值 develop（开发版），trial（体验版），release（正式版）
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    },
    /**
     * 左边区域倒计时
     */
    leftCountDown:function(){
      let that = this;
      that.setData({
        timer: setInterval(function(){
          var lefttime = parseInt((new Date(that.data.leftendTime.replace(/-/g,"/")).getTime() - new Date().getTime()));
          if(lefttime <= 0) {
            that.setData({
              leftCountDown:{day:"00",hour:"00",min:"00",sec:"00"}
            })
            clearInterval(that.data.timer);
            return;
          }

          var d = parseInt(lefttime /1000 /3600 /24);  //天数
          var h = parseInt(lefttime / 1000 / 3600 % 24); //小时
          var m = parseInt(lefttime / 1000 / 60 % 60);    //分钟
          var s = parseInt(lefttime / 1000  % 60);        //当前的秒

          d < 10 ? d = "0" + d : d;
          h < 10 ? h = "0" + h : h;
          m < 10 ? m = "0" + m : m;
          s < 10 ? s = "0" + s : s;

          that.setData({
            leftCountDown:{ day: d, hour: h, min: m, sec:s }
          })
        },1000)
      })
    },
        /**
     * 左边区域倒计时
     */
    rightCountDown:function(){
      let that = this;
      that.setData({
        timer: setInterval(function(){
          var lefttime = parseInt((new Date(that.data.rightendTime.replace(/-/g,"/")).getTime() - new Date().getTime()));
          if(lefttime <= 0) {
            that.setData({
              rightCountDown:{day:"00",hour:"00",min:"00",sec:"00"}
            })
            clearInterval(that.data.timer);
            return;
          }

          var d = parseInt(lefttime /1000 /3600 /24);  //天数
          var h = parseInt(lefttime / 1000 / 3600 % 24); //小时
          var m = parseInt(lefttime / 1000 / 60 % 60);    //分钟
          var s = parseInt(lefttime / 1000  % 60);        //当前的秒

          d < 10 ? d = "0" + d : d;
          h < 10 ? h = "0" + h : h;
          m < 10 ? m = "0" + m : m;
          s < 10 ? s = "0" + s : s;

          that.setData({
            rightCountDown:{ day: d, hour: h, min: m, sec:s }
          })
        },1000)
      })
    },
  }
})
